package com.hyzx.qbasic.admin.domain.repository.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hyzx.qbasic.admin.model.po.BusinessLogPO;
import com.hyzx.qbasic.admin.model.po.ErrorLogPO;
import com.hyzx.qbasic.admin.model.po.UserLoginLogPO;
import com.hyzx.qbasic.admin.model.vo.req.admin.GetBusinessLogListReq;
import com.hyzx.qbasic.admin.model.vo.req.admin.GetErrorLogListReq;
import com.hyzx.qbasic.admin.domain.dao.LogMapper;
import com.hyzx.qbasic.admin.domain.repository.ILogRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * Created by 黄江华 on 2017/9/22
 */

@Repository
public class LogRepositoryImpl implements ILogRepository {

    @Autowired
    private LogMapper logMapper;

    @Override
    public boolean addUserLoginLog(UserLoginLogPO logInfo) {
        return logMapper.addUserLoginLog(logInfo) > 0;
    }

    @Override
    public boolean addBusinessLog(BusinessLogPO logInfo) {
        return logMapper.addBusinessLog(logInfo) > 0;
    }

    @Override
    public PageInfo<BusinessLogPO> getBusinessLogList(GetBusinessLogListReq req) {
        PageInfo pageInfo = PageHelper.startPage(req.getPageNum(), req.getPageSize())
                .doSelectPageInfo(
                        () -> logMapper.getBusinessLogList(req.getBeginTime(), req.getEndTime(),
                                req.getMinResponseTime(), req.getMaxResponseTime()));

        return pageInfo;
    }

    @Override
    public boolean addErrorLog(ErrorLogPO logInfo) {
        return logMapper.addErrorLog(logInfo) > 0;
    }

    @Override
    public PageInfo<ErrorLogPO> getErrorLogList(GetErrorLogListReq req) {
        PageInfo pageInfo = PageHelper.startPage(req.getPageNum(), req.getPageSize())
                .doSelectPageInfo(
                        () -> logMapper.getErrorLogList(req.getBeginTime(), req.getEndTime()));

        return pageInfo;
    }

    @Override
    public ErrorLogPO getErrorLogDetail(int logId) {
        return logMapper.getErrorLogDetail(logId);
    }
}
